package com.tegtech.tools.aliyun.support;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.push.model.v20160801.PushRequest;
import com.aliyuncs.push.model.v20160801.PushResponse;
import com.tegtech.tools.aliyun.properties.AliyunProperties;

import lombok.extern.slf4j.Slf4j;

/**
 * 移动推送
 * https://help.aliyun.com/document_detail/48048.html?spm=a2c4g.11174283.6.626.2d006d16KGn58x
 * @Author Jun
 * @date 2021年5月27日 下午4:27:53
 */
@Slf4j
public class MobilePush {
	
	// 地域
	private static final String REGIONID = "cn-hangzhou";

	/**
	 * 推送
	 * @param deviceId		推送设备别名
	 * @param deviceType	推送设备类型(iOS/ANDROID)
	 * @param title			通知标题
	 * @param body			通知内容
	 */
	public static void push(String deviceId, String deviceType, String title, String body) {
        DefaultProfile profile = DefaultProfile.getProfile(REGIONID, AliyunProperties.getAccessId(), AliyunProperties.getAccessSecret());
        IAcsClient client = new DefaultAcsClient(profile);

        PushRequest request = new PushRequest();
        request.setSysRegionId(REGIONID);
        request.setAppKey(AliyunProperties.getPushAppKey());
        request.setPushType("NOTICE");
        request.setDeviceType(deviceType);
        request.setTarget("ALIAS");
        request.setTargetValue(deviceId);
        request.setTitle(title);
        request.setBody(body);

        try {
            PushResponse response = client.getAcsResponse(request);
            log.info("MobilePush 推送成功，response：{}", response);
        } catch (Exception e) {
            log.error("MobilePush Fail，Error：{}", e);
        }

    }
	
}
